class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

/**
 * 2331. 计算布尔二叉树的值
 * https://leetcode.cn/problems/evaluate-boolean-binary-tree/description/
 */
class Solution {
    public boolean evaluateTree(TreeNode root) {
        boolean ret = dfs(root);
        return ret;
    }
    public boolean dfs(TreeNode root) {
        if(root.left == null && root.right == null) return root.val == 1 ? true : false;
        boolean left = dfs(root.left);
        boolean right = dfs(root.right);
        if(root.val == 2) return left | right;
        else return left & right;
    }
}